﻿using System.Collections.ObjectModel;
using System.Windows;
using FU.Capstones.IMS.Web.Common;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.DataContracts;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.EventArgs;
using FU.Capstones.IMS.Web.Silverlight.Models;
using Microsoft.Practices.Prism.Commands;
using Navigation.Silverlight;

namespace FU.Capstones.IMS.Web.Silverlight.ViewModels
{
    public class MaterialExportViewModel:ViewModelBase
    {
        public MaterialExportViewModel()
        {
             Model = new MaterialExportModel();
            ((MaterialExportModel)Model).GetSingleWarehouseCompleted += DoGetSingleWarehouseCompleted;
            CurrentMaterial = new Material();
            CanEditOrAdd = CurrentUser.CanAddOrderMaterial || CurrentUser.CanEditOrderMaterial; 
            CanAdd = CurrentUser.CanAddOrderMaterial;

        }

        void DoGetSingleWarehouseCompleted(object sender, SingleFeedbackCompletedEventArgs<Warehouse> e)
        {
            if (e.Error != null)
            {
                Message = e.Result.Message;
                MessageVisibility = Visibility.Visible;
            }
            else
            {
                Message = e.Result.Message;
                if (!e.Result.IsSuccessful)
                {
                    MessageVisibility = Visibility.Visible;
                }
                else
                {
                    MessageVisibility = Visibility.Collapsed;
                    MaterialCollection = e.Result.Data.MaterialCollection;
                    Count = MaterialCollection.Count;

                    if (SavedPage.SavedCollectionMaterial != null)
                    {
                       if (CollectionMaterial.Count > 0 && CollectionMaterial.Count <= Count)
                        {
                            MaterialCollection = SavedPage.SavedCollectionMaterial;
                        }
                        
                    }

                }
            }
            ServiceCallCounter -= WebConstants.MagicNumer;
        }


        #region Declare command
        //Save command use to save collection material order to database
        private DelegateCommand<object> saveCommand;
        public DelegateCommand<object> SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new DelegateCommand<object>(DoSaveCommand);
                }
                return saveCommand;
            }
        }
         
        private DelegateCommand<object> cancelCommand;
        public DelegateCommand<object> CancelCommand
        {
            get
            {
                if (cancelCommand == null)
                {
                    cancelCommand = new DelegateCommand<object>(DoBackCommand);
                }
                return cancelCommand;
            }
        }

        //Reset command use to reset list of material
        private DelegateCommand<object> resetCommand;
        public DelegateCommand<object> ResetCommand
        {
            get
            {
                if (resetCommand == null)
                {
                    resetCommand = new DelegateCommand<object>(DoResetCommand);
                }
                return resetCommand;
            }
        }

        #endregion

        #region implement command
        
        private void DoSaveCommand(object obj)
        {
            if (CurrentMaterial.MaterialID != 0 && TotalQuantity > 0 && TotalQuantity <= CurrentMaterial.Quantity)
            {
                NewMaterial = CurrentMaterial;
                NewMaterial.Quantity = TotalQuantity;
                NewMaterial.PricePerUnit = PricePerUnit;
                CollectionMaterial.Add(newMaterial);
                SavedPage.CollectionMaterial = CollectionMaterial;
                for (int i = 0; i < MaterialCollection.Count; i++)
                {
                    if (MaterialCollection[i].MaterialID == newMaterial.MaterialID)
                    {
                        MaterialCollection.RemoveAt(i);
                    }
                }
                SavedPage.SavedCollectionMaterial = MaterialCollection;
                NavigationData ndMaterial = new NavigationData();
                ndMaterial.NavigationUri = WebConstants.NavigationPages.MaterialImport;
                ndMaterial.Data = SavedPage;

                NavigationManager.NavigateTo(WebConstants.NavigationPages.ExportMaterialOrder, ndMaterial);
            }
            else
            {
                if (CurrentMaterial.MaterialID == 0)
                {
                    ErrorMessage = "Bạn chưa chọn nguyên vật liệu";
                }
                else
                {
                    if (TotalQuantity > 0)
                    {
                        if (TotalQuantity > CurrentMaterial.Quantity)
                        {
                            ErrorMessage = "Không đủ nguyên vật liệu trong kho";

                        } 
                    }
                    else
                    {
                        ErrorMessage = "Bạn chưa điền số lượng nguyên vật liệu";
                    }
                    
                }
            }

        }
        private void DoBackCommand(object obj)
        {
            NavigationData ndMaterial = new NavigationData();
            ndMaterial.NavigationUri = WebConstants.NavigationPages.MaterialExport;
            ndMaterial.Data = SavedPage;
            NavigationManager.NavigateTo(WebConstants.NavigationPages.ExportMaterialOrder, ndMaterial);
        }
        private void DoResetCommand(object obj)
        {
            TotalQuantity = 0;
            PricePerUnit = 0;
        }

        #endregion

        #region biding data to combobox
        private ObservableCollection<Material> materialCollection;
        public ObservableCollection<Material> MaterialCollection
        {
            get { return materialCollection; }
            set
            {
                if (!ReferenceEquals(materialCollection, value))
                {
                    materialCollection = value;
                    RaisePropertyChanged(() => MaterialCollection);
                }
            }
        }
        #endregion

        #region Declare TotalQuantity

        private int totalQuantity;
        public int TotalQuantity
        {
            get { return totalQuantity; }
            set 
            { 
                totalQuantity = value;
                RaisePropertyChanged(()=>TotalQuantity);
            }
        }
        private double pricePerUnit;
        public double PricePerUnit
        {
            get { return pricePerUnit; }
            set
            {
                pricePerUnit = value;
                RaisePropertyChanged(() => PricePerUnit);

            }
        }

        #endregion

        #region NewMaterial

        private Material newMaterial;
        public Material NewMaterial
        {
            get { return newMaterial; }
            set
            {
                if (!ReferenceEquals(newMaterial, value))
                {
                    newMaterial = value;
                    RaisePropertyChanged(() => NewMaterial);
                }
            }
        }

        #endregion

        #region Declare Material

        private Material currentMaterial;
        public Material CurrentMaterial
        {
            get { return currentMaterial; }
            set
            {
                if (!ReferenceEquals(currentMaterial,value))
                {
                    currentMaterial = value;
                    RaisePropertyChanged(()=>CurrentMaterial);
                }
            }
        }

        #endregion

        #region ErrorMessage

        private string errorMessage;
        public string ErrorMessage
        {
            get { return errorMessage; }
            set
            {
                if (!ReferenceEquals(errorMessage, value))
                {
                    errorMessage = value;
                    RaisePropertyChanged(() => ErrorMessage);

                }

            }
        }

        #endregion

        private int count;
        public int Count
        {
            get { return count; }
            set
            {
                count = value;
                RaisePropertyChanged(() => Count);

            }
        }

        private ExportMaterialOrderViewModel savedPage;
        public ExportMaterialOrderViewModel SavedPage
        {
            get { return savedPage; }
            set
            {
                if (!ReferenceEquals(savedPage, value))
                {
                    savedPage = value;
                    RaisePropertyChanged(() => SavedPage);
                }
            }
        }
        #region CollectionMaterial
        private ObservableCollection<Material> collectionMaterial;
        public ObservableCollection<Material> CollectionMaterial
        {
            get { return collectionMaterial; }
            set
            {
                if (!ReferenceEquals(collectionMaterial, value))
                {
                    collectionMaterial = value;
                    RaisePropertyChanged(() => CollectionMaterial);
                }
            }
        }
        #endregion
        #region Implementation of INavigationAware

        public override void OnNavigate(object extraData)
        {

            var nd = (NavigationData)extraData;
            if (nd != null)
            {
                if (nd.NavigationUri.Equals(WebConstants.NavigationPages.ExportMaterialOrder))
                {
                    if (nd.Data != null)
                    {
                        if (nd.Data.GetType().Equals(typeof(ExportMaterialOrderViewModel)))
                        {
                            SavedPage = (ExportMaterialOrderViewModel)nd.Data;
                            ServiceCallCounter += ((MaterialExportModel)Model).GetSingleWarehouseAsync(SavedPage.SelectedWarehouse.WarehouseID);
                            CollectionMaterial = SavedPage.CollectionMaterial;
                        }
                    }

                }
            }
            else
            {
                CollectionMaterial = new ObservableCollection<Material>();
                SavedPage = new ExportMaterialOrderViewModel();
                
            }

        }

        public override void OnLeave()
        {
            //will be implemented in the sub classes
        }

        #endregion

    }
}
